package com.wushi.lenovo.asia5b.app_ui.inbox.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.mvp.base.BaseFragmentPagerAdapter;
import com.mvp.base.BaseMvpActivity;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.inbox.mvpview.InboxActivityView;
import com.wushi.lenovo.asia5b.app_di.inbox.presenter.InboxActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeInboxEvent;
import com.wushi.lenovo.asia5b.app_model.inbox.InboxMsgType;
import com.wushi.lenovo.asia5b.app_ui.inbox.fragment.InboxMsgFragment;
import com.wushi.lenovo.asia5b.app_ui.login.activity.LoginUserActivity;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2019/1/7.
 * 收件箱
 */
public class InboxActivity extends BaseMvpActivity<InboxActivityView, InboxActivityPresenter> implements InboxActivityView {
    @BindView(R.id.vp_inbox)
    ViewPager mViewPager;
    @BindView(R.id.tablayout_inbox)
    TabLayout tabLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_inbox;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (!UserRepository.isLogin()) {
            startActivity(new Intent(this, LoginUserActivity.class));
            finish();
        }
        initView();
        initData();
        initEvent();


    }

    @Override
    protected void initView() {
        tabLayout.removeAllTabs();
        tabLayout.addTab(tabLayout.newTab().setCustomView(tabView(getString(R.string.inbox_orders))));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tabView(getString(R.string.inbox_inbox))));

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(InboxMsgFragment.newInstance(this, InboxMsgType.order));
        fragments.add(InboxMsgFragment.newInstance(this, InboxMsgType.inbox));

        mViewPager.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setOffscreenPageLimit(2);
    }

    private View tabView(String tabName) {
        View view = View.inflate(this, R.layout.item_inbox_tabitem, null);
        TextView tv_inbox_inboxtitle = view.findViewById(R.id.tv_inbox_inboxtitle);
        TextView tv_inbox_inboxcount = view.findViewById(R.id.tv_inbox_inboxcount);
        tv_inbox_inboxtitle.setText(tabName);
        tv_inbox_inboxcount.setText("0");
        return view;
    }


    public void setTabCount(int index, int count) {
        try {
            TextView tv_inbox_inboxcount = tabLayout.getTabAt(index).getCustomView().findViewById(R.id.tv_inbox_inboxcount);
            EyeUtils.setNums(tv_inbox_inboxcount, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {
        if(UserRepository.isLogin()) {
            getMsgCountForOrder();
            getMsgCountForInbox();
        }
    }

    private void getMsgCountForOrder() {
        getPresenter().getMsgCountForOrder();
    }

    private void getMsgCountForInbox() {
        getPresenter().getMsgCountForInbox();
    }

    @Override
    protected void initEvent() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                tabLayout.setScrollPosition(i, 0f, true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


    @Override
    public void getMsgCountForOrderResult(int count) {
        setTabCount(0, count);
    }

    @Override
    public void getMsgCountForInboxResult(int count_unread) {
        setTabCount(1, count_unread);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeInboxEvent(ChangeInboxEvent event) {
        if (event != null) {
            if (event.type == InboxMsgType.order) {
                getMsgCountForOrder();
            } else {
                getMsgCountForInbox();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
